package com.lagou.rocketmq.consumer;

import com.lagou.rocketmq.dao.GoodsQuantityDao;
import com.lagou.rocketmq.service.GoodsQuantityServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @ClassName Consumer
 * @Description 订单相关消费者
 * @Author xsq
 * @Date 2020/11/25 16:29
 **/
@Slf4j
@Component
@RocketMQMessageListener(topic = "seckill", consumerGroup = "seckill-consumer")
public class OrderConsumer implements RocketMQListener<String> {
    @Autowired
    private GoodsQuantityServiceImpl goodsQuantityServiceImpl;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override

    public void onMessage(String id) {
        if (id != null) {
            //减库存,下单
            goodsQuantityServiceImpl.addGoods(id);
            //使用rocketmq延迟队列进行支付状态判断,延迟到期,消费端进行消费,判断如果订单还没有支付则把状态改为支付过期
            rocketMQTemplate.syncSend("pay_check", MessageBuilder.withPayload(id).build(), 50000, 1);
        }
    }
}
